package com.atguigu.linklist;


import org.omg.CORBA.DoubleHolder;

import java.util.Stack;

//自定义单链表结构，头节点不存储数据，尾节点指向null，链表的核心就是建立一个辅助指针，就行遍历、增加、update等等
public class DoubleLinkListDemo {

    public static void main(String[] args) {
        DoubleLinkList doubleLinkList = new DoubleLinkList();
        DoubleHero doubleHero = new DoubleHero(1, "张作霖");
        DoubleHero doubleHero1 = new DoubleHero(4, "冯天祥");
        DoubleHero doubleHero2 = new DoubleHero(3, "张宗昌");
        DoubleHero doubleHero3 = new DoubleHero(4, "汤玉麟");
        DoubleHero doubleHero4 = new DoubleHero(5, "张学良");

//        doubleLinkList.addLink(doubleHero);
//        doubleLinkList.addLink(doubleHero1);
//        doubleLinkList.addLink(doubleHero2);
//        doubleLinkList.addLink(doubleHero3);
//        doubleLinkList.addLink(doubleHero4);

        doubleLinkList.addLinkOrder(doubleHero);
        doubleLinkList.addLinkOrder(doubleHero1);
        doubleLinkList.addLinkOrder(doubleHero2);
        doubleLinkList.updateList(doubleHero3);
        doubleLinkList.addLinkOrder(doubleHero4);

        doubleLinkList.list();

        System.out.println("删除队列");

        doubleLinkList.delete(1);
        doubleLinkList.delete(4);
        doubleLinkList.list();

    }
}
class DoubleLinkList{

    //定义链表的头部
    private DoubleHero head = new DoubleHero(0,"");


    //插入列表,插入到最后面，要找到最后一个节点
    public void addLink(DoubleHero doubleHero){
        //辅助遍历节点temp
        DoubleHero temp  = head;
        while (true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = doubleHero;
        doubleHero.pre = temp;
    }

    //要找到插入节点的前面一个节点
    public void addLinkOrder(DoubleHero doubleHero){
        DoubleHero temp = head;
        boolean flag = true;
        while (true){
            if (temp.next == null){
                break;
            }

            if(temp.next.no > doubleHero.no){
                break;
            }else if (temp.next.no == doubleHero.no){
                flag=false;
                break;
            }
            temp = temp.next;
        }

        if (flag){
            doubleHero.next = temp.next;
            if (temp.next != null){
                temp.next.pre = doubleHero;
            }
            temp.next = doubleHero;
            doubleHero.pre = temp;
        }else {
            System.out.printf("no %d已存在,无法插入 \n",doubleHero.no);
        }
    }

    public void updateList(DoubleHero doubleHero){
        if (head.next == null){
            System.out.println("链表为空，无法修改");
        }
        DoubleHero temp = head;
        boolean flag = true;
        while (true){
            if (temp.next.no == doubleHero.no){
                temp.next.name = doubleHero.name;
                break;
            }
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
    }

    public void delete(int no){
        if (head.next == null){
            System.out.println("链表为空，无法删除");
        }
        DoubleHero temp = head;
        boolean flag = false;
        while (true){
            if (temp.no == no){
                flag = true;
                break;
            }
            if (temp == null){
                break;
            }
            temp = temp.next;
        }

        if (flag){
            temp.pre.next= temp.next;
            temp.next.pre= temp.pre;
        }else {
            System.out.println("没有找到要删除的节点");
        }
    }

    //展示单链表所有节点
    public void list() {
        //辅助遍历节点temp
        DoubleHero temp  = head.next;
        if (head.next == null){
            System.out.println("链表为空,无内容展示");
        }

        while (true){
            if (temp==null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }

    }

    public int cunLinkList(){
        int cun = 0;
        DoubleHero temp = head.next;
        while (true){
            if (temp==null){
                break;
            }
            cun++;
            temp = temp.next;
        }
        return cun;
    }

    public void getHero(int k) {

        int n = this.cunLinkList();
        DoubleHero temp = head.next;

        for (int i=0;i<n-k;i++){
            temp = temp.next;
        }
        if(n-k<0){
            System.out.printf("无法找到数据");
        }else {
            System.out.println(temp);
        }

    }

    //将单链表翻转
    public void reverseLink(){
        DoubleHero temp = head.next;
        if (temp == null){
            System.out.println("链表为空，无法反转");
            return;
        }
        DoubleHero newHead = new DoubleHero(0,"");
        DoubleHero cur = null;
        while (temp != null){
            cur = temp.next;
            temp.next = newHead.next;
            newHead.next = temp;
            temp = cur;
        }
        head.next = newHead.next;
    }

    //单链表倒序打印
    public void printDesc(){
        DoubleHero temp = head.next;
        if (temp==null){
            System.out.printf("队列为空，无法打印");
            return;
        }
        Stack<DoubleHero> DoubleHeroes = new Stack<>();
        while (temp != null){
            DoubleHeroes.add(temp);
            temp = temp.next;
        }
        while (DoubleHeroes.size()>0){
            DoubleHero pop = DoubleHeroes.pop();
            System.out.println(pop);
        }

    }


}

class DoubleHero{
    int no;
    String name;
    DoubleHero next;
    DoubleHero pre;

    public DoubleHero(int no,String name){
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "DoubleHero{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}